from enum import Enum
from typing import Union, Optional, List, Dict
from pydantic import BaseModel, constr, conint, validator


class GenderEnum(str, Enum):
    """
    性别枚举
    """
    male = "男"
    female = "女"


class PydanticVerifyParam(BaseModel):
    """
    用来学习使用 pydantic 模型验证
    """
    user_name: str  # 基本类型

    @validator("user_name")
    def validateUsername(cls, value:str):
        """
        自定义验证函数
        :param value:
        :return:
        """
        if value.find("傻") > -1:
            raise ValueError("user_name不能包含敏感词")
        return value

    age: conint(ge=10, le=30)   # 整数范围：18<= age <= 30
    password: constr(min_length=6, max_length=10)    # 字符长度
    phone: constr(regex=r'^1\d{10}$')  # 正则验证手机号
    address: Optional[str] = None   # 可选参数
    sex: GenderEnum     # 枚举验证，只能传：男和女
    likes: List[str]    # 值会自动化转成传字符串列表
    scores: Dict[str,float]     # key 会转成字符串，val 会转成浮点型

